package com.example.wananrdroid.fragment.zhufragment.tixi;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wananrdroid.Main2Activity;
import com.example.wananrdroid.R;
import com.example.wananrdroid.adapter.DaohangAdapter;
import com.example.wananrdroid.base.BaseFragment;
import com.example.wananrdroid.bean.NaviBean;
import com.example.wananrdroid.presenter.NaviPresenter;
import com.example.wananrdroid.view.NaviView;

import java.util.ArrayList;

import butterknife.BindView;
import qdx.stickyheaderdecoration.NormalDecoration;

/**
 * A simple {@link Fragment} subclass.
 */
public class DaohangFragment extends BaseFragment<NaviPresenter> implements NaviView {

    @BindView(R.id.daohang_re)
    RecyclerView mDaohangRe;
    private ArrayList<NaviBean.DataBean> dataBeans;
    private DaohangAdapter daohangAdapter;

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        mDaohangRe.setLayoutManager(new LinearLayoutManager(getContext()));
        dataBeans = new ArrayList<>();
        daohangAdapter = new DaohangAdapter(getContext(), dataBeans);
        NormalDecoration normalDecoration = new NormalDecoration() {
            @Override
            public String getHeaderName(int i) {
                return dataBeans.get(i).getName();
            }
        };
        mDaohangRe.addItemDecoration(normalDecoration);
        mDaohangRe.setAdapter(daohangAdapter);
        daohangAdapter.setOnContextClickListener(new DaohangAdapter.OnContextClickListener() {
            @Override
            public void oncontextclicklistener(View v, int position, int i) {
                String link = dataBeans.get(position).getArticles().get(i).getLink();
                Intent intent = new Intent(getContext(), Main2Activity.class);
                intent.putExtra("url",link);
                startActivity(intent);
            }
        });
    }
    @Override
    protected void initPresenter() {
        mPresenter=new NaviPresenter();
        mPresenter.navidata();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_daohang;
    }

    private void initViews(View view) {
        mDaohangRe = view.findViewById(R.id.daohang_re);
    }

    @Override
    public void onSuccess(NaviBean navidata) {
        dataBeans.addAll(navidata.getData());
        daohangAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFali(String error) {

    }

    @Override
    public void showToast(String str) {

    }
}
